package com.chinatechstar.admin.controller;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.chinatechstar.admin.entity.SysOrg;
import com.chinatechstar.admin.service.SysOrgService;
import com.chinatechstar.admin.vo.SysOrgVO;
import com.chinatechstar.component.commons.result.ActionResult;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.utils.ExcelUtils;
import com.chinatechstar.component.commons.validator.InsertValidator;
import com.chinatechstar.component.commons.validator.UpdateValidator;

/**
 * 机构信息的控制层
 */
@Api(value = "机构信息的控制层", tags = {"机构信息"})
@RestController
@RequestMapping("/sysorg")
public class SysOrgController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private SysOrgService sysOrgService;

    /**
     * 查询机构分页
     *
     * @param sysOrgVO 机构前端参数
     * @return
     */
    @ApiOperation(value = "查询机构分页", tags = {"机构信息"})
    @GetMapping(path = "/querySysOrg")
    public ListResult<Object> querySysOrg(SysOrgVO sysOrgVO) {
        Map<String, Object> data = sysOrgService.querySysOrg(sysOrgVO.getCurrentPage(), sysOrgVO.getPageSize(), sysOrgVO.getOrgName(),
                sysOrgVO.getOrgType(), sysOrgVO.getOrgDescription());
        return ResultBuilder.buildListSuccess(data);
    }

    /**
     * 查询机构的树数据
     *
     * @return
     */
    @ApiOperation(value = "查询机构的树数据", tags = {"机构信息"})
    @GetMapping(path = "/querySysOrgTree")
    public ListResult<Object> querySysOrgTree() {
        LinkedHashMap<String, Object> data = sysOrgService.querySysOrgTree();
        return ResultBuilder.buildListSuccess(data);
    }

    /**
     * 查询机构类型的下拉框数据
     *
     * @return
     */
    @ApiOperation(value = "查询机构类型的下拉框数据", tags = {"机构信息"})
    @GetMapping(path = "/queryOrgType")
    public ListResult<Object> queryOrgType() {
        LinkedHashMap<String, Object> data = sysOrgService.queryOrgType();
        return ResultBuilder.buildListSuccess(data);
    }

    /**
     * 新增机构
     *
     * @param sysOrg 机构对象
     * @return
     */
    @ApiOperation(value = "新增机构", tags = {"机构信息"})
    @PostMapping(path = "/addSysOrg")
    public ActionResult addSysOrg(@Validated(InsertValidator.class) @RequestBody SysOrg sysOrg) {
        sysOrgService.insertSysOrg(sysOrg);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 编辑机构
     *
     * @param sysOrg 机构对象
     * @return
     */
    @ApiOperation(value = "编辑机构", tags = {"机构信息"})
    @PutMapping(path = "/updateSysOrg")
    public ActionResult updateSysOrg(@Validated(UpdateValidator.class) @RequestBody SysOrg sysOrg) {
        sysOrgService.updateSysOrg(sysOrg);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 删除机构
     *
     * @param id 机构ID
     * @return
     */
    @ApiOperation(value = "删除机构", tags = {"机构信息"})
    @PostMapping(path = "/deleteSysOrg")
    public ActionResult deleteSysOrg(@RequestParam(name = "id") Long[] id) {
        sysOrgService.deleteSysOrg(id);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 根据查询条件导出机构
     *
     * @param response 响应对象
     * @param paramMap 参数Map
     */
    @ApiOperation(value = "根据查询条件导出机构", tags = {"机构信息"})
    @PostMapping(path = "/exportSysOrg")
    public void exportSysOrg(HttpServletResponse response, @RequestParam Map<String, Object> paramMap) {
        try {
            List<String> headList = Arrays.asList("ID", "机构名称", "机构类型", "机构类型名称", "机构描述", "排序", "上级机构ID", "创建时间");
            List<LinkedHashMap<String, Object>> dataList = sysOrgService.querySysOrgForExcel(paramMap);
            ExcelUtils.exportExcel(headList, dataList, "机构管理", response);
        } catch (Exception e) {
            logger.warn(e.toString());
        }
    }

}
